package com.yuedou.api.service.platfom.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuedou.api.dao.platform.AdBannerMapper;
import com.yuedou.api.entity.platform.AdBanner;
import com.yuedou.api.service.base.impl.BaseServiceImpl;
import com.yuedou.api.service.platfom.AdBannerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdBannerServiceImpl extends BaseServiceImpl<AdBannerMapper, AdBanner> implements AdBannerService {

    @Resource
    private AdBannerMapper adBannerMapper;

    @Value("${web.imgPrefixOss}")
    private String imgPrefixOss;//图片地址

    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Override
    public AdBanner getBannerByAdType(Integer ad_type) {
        AdBanner banner = adBannerMapper.selectOneByColumn("ad_type",ad_type);
        if(banner!=null ){
            String img_key = banner.getImgUrl()!=null ? banner.getImgUrl():"";
            if(!img_key.equals("")){
                banner.setImgUrl(imgPrefixOss + img_key);
            }
        }
        return banner;
    }


    @Override
    public List<AdBanner> getList(int ad_type) {
        List<AdBanner> banners = new ArrayList<>();
        try{
            QueryWrapper<AdBanner> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("ad_type",ad_type);
            qw.orderByAsc("sort");

            String key = "ad_banner_" + ad_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    banners = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), AdBanner.class);
                } else {
                    banners = adBannerMapper.selectList(qw);
                    if(CollUtil.isNotEmpty(banners)){
                        banners.forEach(banner->{
                            if(StrUtil.isNotEmpty(banner.getImgUrl())){
                                banner.setImgUrl(imgPrefixOss + banner.getImgUrl());
                            }
                        });
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(banners));
                }
            }else{
                banners = adBannerMapper.selectList(qw);
                if(CollUtil.isNotEmpty(banners)){
                    banners.forEach(banner->{
                        if(StrUtil.isNotEmpty(banner.getImgUrl())){
                            banner.setImgUrl(imgPrefixOss + banner.getImgUrl());
                        }
                    });
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(banners));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return banners;
    }

    @Override
    public void resetBannerData(int ad_type) {
        List<AdBanner> banners = new ArrayList<>();
        try{
            QueryWrapper<AdBanner> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("ad_type",ad_type);
            qw.orderByAsc("sort");
            String key = "ad_banner_" + ad_type;
            banners = adBannerMapper.selectList(qw);
            if(CollUtil.isNotEmpty(banners)){
                banners.forEach(banner->{
                    if(StrUtil.isNotEmpty(banner.getImgUrl())){
                        banner.setImgUrl(imgPrefixOss + banner.getImgUrl());
                    }
                });
            }
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(banners));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
